from PyQt6.QtWidgets import (
    QWidget, QHBoxLayout, QLabel, QPushButton, QFrame
)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon, QPixmap
import os

class TitleBar(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.setObjectName("titleBar")
        self.setup_ui()
        
    def setup_ui(self):
        layout = QHBoxLayout(self)
        layout.setContentsMargins(10, 0, 0, 0)
        layout.setSpacing(0)
        
        # 窗口标题
        self.title_label = QLabel(self.parent.config["window"]["title"])
        self.title_label.setObjectName("titleLabel")
        
        # 按钮容器
        btn_container = QWidget()
        btn_container.setObjectName("titleBtnContainer")
        btn_layout = QHBoxLayout(btn_container)
        btn_layout.setContentsMargins(0, 0, 0, 0)
        btn_layout.setSpacing(0)
        
        # 创建按钮
        self.min_button = TitleButton("minimize", "最小化")
        self.max_button = TitleButton("maximize", "最大化")
        self.close_button = TitleButton("close", "关闭")
        
        # 添加按钮到布局
        btn_layout.addWidget(self.min_button)
        btn_layout.addWidget(self.max_button)
        btn_layout.addWidget(self.close_button)
        
        # 添加所有元素到主布局
        layout.addWidget(self.title_label)
        layout.addStretch()
        layout.addWidget(btn_container)
        
        # 连接信号
        self.min_button.clicked.connect(self.parent.showMinimized)
        self.max_button.clicked.connect(self.toggle_maximize)
        self.close_button.clicked.connect(self.parent.close)
        
    def toggle_maximize(self):
        if self.parent.isMaximized():
            self.parent.showNormal()
            self.max_button.set_icon("maximize")
        else:
            self.parent.showMaximized()
            self.max_button.set_icon("restore")
            
    def mousePressEvent(self, event):
        if event.button() == Qt.MouseButton.LeftButton:
            self.parent._drag_pos = event.globalPosition().toPoint()
            
    def mouseMoveEvent(self, event):
        if hasattr(self.parent, '_drag_pos'):
            if self.parent.isMaximized():
                self.parent.showNormal()
                self.max_button.set_icon("maximize")
            diff = event.globalPosition().toPoint() - self.parent._drag_pos
            newpos = self.parent.pos() + diff
            self.parent.move(newpos)
            self.parent._drag_pos = event.globalPosition().toPoint()
            
    def mouseDoubleClickEvent(self, event):
        if event.button() == Qt.MouseButton.LeftButton:
            self.toggle_maximize()


class TitleButton(QPushButton):
    def __init__(self, icon_name, tooltip):
        super().__init__()
        self.setObjectName(f"titleBtn{icon_name.capitalize()}")
        self.setToolTip(tooltip)
        self.setFixedSize(QSize(46, 32))
        self.set_icon(icon_name)
        
    def set_icon(self, icon_name):
        # 获取图标路径
        current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        icon_path = os.path.join(current_dir, "resources", "icons", f"{icon_name}.png")
        
        if os.path.exists(icon_path):
            self.setIcon(QIcon(icon_path))
            self.setIconSize(QSize(10, 10)) 